package com.bnzj.cloud.business.core.persistence.repository;

import com.bnzj.cloud.business.core.persistence.entity.SysResource;
import com.bnzj.core.persistence.repository.BaseRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import javax.persistence.Tuple;
import java.util.List;
import java.util.Optional;

@Repository
public interface SysResourceRepository extends BaseRepository<SysResource, Long> {

    @Query(value = "SELECT distinct r.* from bnz_sys_resource r"
            + " LEFT JOIN bnz_sys_role_resource rr on rr.resource_id = r.id"
            + " LEFT JOIN bnz_sys_role role on role.id=rr.role_id"
            + " where r.type in ('0') and role.code in :ids order by CHAR_LENGTH(r.path_index), r.sequence", nativeQuery = true)
    List<SysResource> findByRuleId(@Param(value = "ids") List<String> ids);

    @Query(value = "select DISTINCT s.* from bnz_sys_resource s" +
            " left join bnz_sys_role_resource rs on s.id = rs.resource_id" +
            " left join bnz_sys_role r on rs.role_id = r.id" +
            " left join bnz_sys_role_user ru on r.id = ru.role_id" +
            " left join bnz_sys_user u on ru.user_id = u.id" +
            " where u.account = ?1 and s.url = ?2", nativeQuery = true)
    List<SysResource> findByUserAndUrl(String account, String url);

    @Query(value = "select DISTINCT s.* from bnz_sys_resource s" +
            " left join bnz_sys_role_resource rs on s.id = rs.resource_id" +
            " left join bnz_sys_role r on rs.role_id = r.id" +
            " left join bnz_sys_role_user ru on r.id = ru.role_id" +
            " left join bnz_sys_user u on ru.user_id = u.id" +
            " where u.account = ?1 and s.pid in ?2 order by s.sequence", nativeQuery = true)
    List<SysResource> findByUserAndPids(String account, long... pids);

    @Query(value = "select DISTINCT s.* from bnz_sys_resource s" +
            " where s.type = 0 and s.pid in ?1 order by s.sequence", nativeQuery = true)
    List<SysResource> findByUserAdmin(long... pids);

    @Query(value = "select DISTINCT s.* from bnz_sys_resource s" +
            " left join bnz_sys_role_resource rs on s.id = rs.resource_id" +
            " left join bnz_sys_role r on rs.role_id = r.id" +
            " left join bnz_sys_role_user ru on r.id = ru.role_id" +
            " left join bnz_sys_user u on ru.user_id = u.id" +
            " where u.account = ?1 and s.pid in (select s.id from bnz_sys_resource s" +
            " left join bnz_sys_role_resource rs on s.id = rs.resource_id" +
            " left join bnz_sys_role r on rs.role_id = r.id" +
            " left join bnz_sys_role_user ru on r.id = ru.role_id" +
            " left join bnz_sys_user u on ru.user_id = u.id" +
            " where u.account = ?1 and s.url = ?2) order by s.sequence", nativeQuery = true)
    List<SysResource> findChildByUserAndUrl(String account, String url);

    /**
     * @param pid
     * @param url
     * @return
     */
    Optional<SysResource> findByPidAndUrl(long pid, String url);

    /**
     * @return
     */
    @Query(value = "select re.url, GROUP_CONCAT(ro.`code`) code" +
            " from bnz_sys_resource re left join bnz_sys_role_resource rr on re.id = rr.resource_id " +
            " LEFT JOIN bnz_sys_role ro on rr.role_id = ro.id and ro.enabled = 1" +
            " GROUP BY re.url", nativeQuery = true)
    List<Tuple> findAllUrlAndRoleCode();

    @Query(value = "select s.* from bnz_sys_resource s" +
            " left join bnz_sys_role_resource rs on s.id = rs.resource_id" +
            " left join bnz_sys_role r on rs.role_id = r.id" +
            " left join bnz_sys_role_user ru on r.id = ru.role_id" +
            " left join bnz_sys_user u on ru.user_id = u.id" +
            " where u.account = ?1 ", nativeQuery = true)
    List<SysResource> findAllByUserAccount(String account);


    @Query(value = "SELECT srr.* FROM bnz_sys_resource srr WHERE EXISTS (SELECT  rr.resource_id FROM bnz_sys_role_resource rr WHERE rr.role_id in (SELECT ru.role_id from bnz_sys_role_user ru left join bnz_sys_user sr on ru.user_id = sr.id WHERE sr.account=?1) AND srr.id=rr.resource_id) AND srr.type=?2", nativeQuery = true)
    List<SysResource> findCurrentUserButton(String account, Integer type);

    @Query(value = "SELECT srr.* FROM bnz_sys_resource srr WHERE srr.type=?1", nativeQuery = true)
    List<SysResource> findCurrentUserButtonAdmin(Integer type);
}
